//
//  MyConnection.m
//  小胖兔旅行指南
//
//  Created by dlios on 15/4/13.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "MyConnection.h"
#import "Reachability.h"
#import "ConnectTools.h"

@implementation MyConnection

-(void)dealloc
{
    Block_release(_finishBlock);
    [_requestUrl release];
    [_receiveData release];
    
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.receiveData = [NSMutableData data];
    }
    return self;
}

#pragma mark -
#pragma mark 返回网络是否连通
+ (BOOL)netWorkReachability
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            return YES;
        }
            break;
        case ReachableViaWWAN:
        {
            return YES;
        }
            break;
        default:
            break;
    }
}

#pragma mark -
#pragma mark 判断有网没网,进行相应的请求操作
+ (void)connectWithUrl:(NSString *)urlStr parmaters:(NSDictionary *)parmaters finishBlock:(void(^)(NSData *data))block
{
    BOOL judge = [MyConnection netWorkReachability];
    if (judge) {//有网的状态
        MyConnection *conn = [[MyConnection alloc] init];
        conn.finishBlock = block;
        [conn startWithUrl:urlStr parmaters:parmaters];
    } else {//没网的状态
        NSString *url = [MyConnection urlWithName:urlStr parmaters:parmaters];
        NSData *data = [ConnectTools readWithName:url directory:NSLibraryDirectory];
        block(data);
    }
}

#pragma mark -
#pragma mark 网址拼接
+ (NSString *)urlWithName:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{

    NSString *par = @"";
    for (NSString *key in [parmaters allKeys]) {
        if ([par length] == 0) {
            par = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        } else {
            par = [NSString stringWithFormat:@"%@&%@=%@", par, key, [parmaters objectForKey:key]];
        }
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, par];
    return urlStr;
}

#pragma mark -
#pragma mark 进行网络请求
- (void)startWithUrl:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    urlStr = [MyConnection urlWithName:urlStr parmaters:parmaters];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.requestUrl = urlStr;
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

#pragma mark -
#pragma mark 服务器的response
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
}

#pragma mark -
#pragma mark 接收网络请求的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

#pragma mark -
#pragma mark 结束接收网路请求的数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.finishBlock(self.receiveData);
    [ConnectTools writeWithName:self.requestUrl data:self.receiveData directory:NSLibraryDirectory];
}

#pragma mark -
#pragma mark 打印网络请求Error
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error = %@", error);
}

@end
